﻿/* dependance js_fnBase.js */
var Suggest = function (stringUrl) {
    this.theme = "/css/suggest.css";
	this.stringKeySpliter = ":"
	this.stringRecordSpliter = "|";
	this.stringSystemKeys = "s,sh,sz,0,1,2,3,4,5,6,7,8,9";
	this.arraySystemKeys = new Array();
	this.arrayPrepareKeys = new Array();
	this.intPrepareKeysMaxLength = 50;
	this.stringData = new String();
	
	this.getNearestData = function (stringKey) {
		if (this.arrayPrepareKeys.length == 0) {
			return new String();
		}
		var arrayContainers = new Array();
		for (var i =0; i < this.arraySystemKeys.length; i++) {
			if (this.arraySystemKeys[i][0] == stringKey) {
				return this.arraySystemKeys[i][1];
			}
			if (stringKey.match(new RegExp("^" + this.arraySystemKeys[i][0], "igm")) != null) {
				arrayContainers.push(this.arraySystemKeys[i]);
			}
		}
		for (var i = 0; i < this.arrayPrepareKeys.length; i++) {
			if (this.arrayPrepareKeys[i][0] == stringKey) {
				return this.arrayPrepareKeys[i][1];
			}
			if (stringKey.match(new RegExp("^" + this.arrayPrepareKeys[i][0], "igm")) != null) {
				arrayContainers.push(this.arrayPrepareKeys[i]);
			}
		}
		if (arrayContainers.length == 0) {
			return new String();
		}
		else {
			arrayContainers.sort(
				function (arrayA, arrayB) {
					return arrayB[0].length - arrayA[0].length;
				}
			);
			return arrayContainers[0][1];
		}
	};


	this.getResult = function (stringData, stringKey) {
		var stringRegExpSystem = "$()*+.[?\^{|";
		var stringKeySpliter = (stringRegExpSystem.indexOf(this.stringKeySpliter) < 0 ? "" : "\\") + this.stringKeySpliter;
		var stringRecordSpliter = (stringRegExpSystem.indexOf(this.stringRecordSpliter) < 0 ? "" : "\\") + this.stringRecordSpliter;
		var arrayMatchResult = stringData.match(new RegExp("" + stringRecordSpliter + (isNaN(parseInt(stringKey)) ? "" : "(s[hz])?") + stringKey + "[^\\" + stringRecordSpliter + "|" + stringKeySpliter + "]*" + stringKeySpliter + "[^\\" + stringRecordSpliter + "|" + stringKeySpliter + "|\n]*", "igm"));
		return arrayMatchResult == null ? new Array() : arrayMatchResult;
	};
	

	this.getQuickResult = function (stringKey) {
		stringKey = stringKey.split(this.stringKeySpliter).join("").split(this.stringRecordSpliter).join("");
		if (stringKey == "") {
			return new Array();
		}
		var stringNearestData = this.getNearestData(stringKey);
		var arrayResult = stringNearestData == "" ?  this.getResult(this.stringData, stringKey) : this.getResult(stringNearestData, stringKey);
		arrayResult = arrayResult == null ? new Array() : arrayResult;
		var booleanIsInSystemKeys = false;
		for (var i = 0; i < this.arraySystemKeys.length; i++) {
			if (this.arraySystemKeys[i][0] == stringKey) {
				booleanIsInSystemKeys = true;
				break;
			}
		}
		var booleanIsInPrepareKeys = false;
		for (var i = 0; i < this.arrayPrepareKeys.length; i++) {
			if (this.arrayPrepareKeys[i][0] == stringKey) {
				booleanIsInPrepareKeys = true;
				break;
			}
		}
		if (!booleanIsInSystemKeys && !booleanIsInPrepareKeys) {
			this.arrayPrepareKeys.push(new Array(stringKey, arrayResult.join("")));
			if (this.arrayPrepareKeys.length > this.intPrepareKeysMaxLength) {
				this.arrayPrepareKeys.sort(
					function (arrayA, arrayB) {
						return arrayA[0].length - arrayB[0].length;
					}
				);
				this.arrayPrepareKeys.pop();
			}
		}
		return arrayResult;
	};
	this.load = function (stringKey) {
		if (stringKey.indexOf(",") != -1 && stringKey.indexOf(",") != 0 && stringKey.indexOf(",") != stringKey.length - 1) {
			var arrayStringKey = stringKey.split(",");
			stringKey = arrayStringKey[arrayStringKey.length - 1];
		}
		if (stringKey.indexOf("\\") != -1) {
			return new Array();
		}
		var stringRegExpSystem = "$()*+.[?^{|";
		for (var i = 0; i < stringRegExpSystem.length; i++) {
			if (stringKey.indexOf(stringRegExpSystem.substr(i, 1)) != -1) {
				return new Array();
			}
		}
		var stringMarket = new String();
		var arrayQuickResult = this.getQuickResult(stringKey);
		arrayQuickResult.length = arrayQuickResult.length > 10 ? 10 : arrayQuickResult.length;
		return arrayQuickResult;
	};
	this.show = function () {
		var element = arguments.callee.element;
		if (!element.booleanScan) {
			return;
		}
		if (element.stringLastValue != element.value && element.value != "代码/名称/拼音") {
			element.line = null;
			element.stringLastValue = element.value;
			var arrayResult = element.suggest.load(element.value);
			if (arrayResult.length > 0) {
				element.divHint.style.display = "block";
				var arrayPosition = GetOffsetPos(element);
				element.divHint.style.left = arrayPosition[0] + "px";
				element.divHint.style.top = arrayPosition[1] + "px";
				element.divHint.style.marginTop = element.clientHeight + 1 + "px";
				element.divHint.style.width = (element.clientWidth < 200 ? 200 : element.clientWidth) + "px";
				var tableContainer = document.createElement("table");
				tableContainer.className = "tableSuggest";
				tableContainer.cellPadding = 0;
				tableContainer.cellSpacing = 0;
				var trHeader = tableContainer.insertRow(0);
				trHeader.className = "trHeader";
				var tdKey = trHeader.insertCell(0);
				tdKey.innerHTML = "选项";
				var tdCode = trHeader.insertCell(1);
				tdCode.innerHTML = "代码";
				var tdName = trHeader.insertCell(2);
				tdName.innerHTML = "名称";
				for (var i = 0; i < arrayResult.length; i++) {
					if (isNaN(parseInt(i))) {
						continue;
					};
					var arrayRecord = arrayResult[i].replace("|", "").split(":");
					var arrayCodeAndName = arrayRecord[1].split("-");
					var trRecord = tableContainer.insertRow(parseInt(i) + 1);
					var tdKey = trRecord.insertCell(0);
					tdKey.innerHTML = arrayRecord[0];
					var tdCode = trRecord.insertCell(1);
					//~ tdCode.innerHTML = arrayCodeAndName[0] + arrayCodeAndName[1];
					tdCode.innerHTML = arrayCodeAndName[1];
					var tdName = trRecord.insertCell(2);
					tdName.innerHTML = arrayCodeAndName[2];
					//~ trRecord.stringFullCode = arrayCodeAndName[0] + arrayCodeAndName[1];
					trRecord.stringFullCode = arrayCodeAndName[1];
					trRecord.stringFullName = arrayCodeAndName[2];
					trRecord.stringCodeAndName = arrayCodeAndName[2] + "(" + arrayCodeAndName[1] + ")";
					trRecord.inputTarget = element;
					trRecord.onmouseover = function () {
						this.inputTarget.overLine = this;
						this.className = this.inputTarget.line == this ? "overSelectedLine" : "overLine";
					};
					trRecord.onmouseout = function () {
						this.inputTarget.overLine = null;
						this.className = this.inputTarget.line == this ? "selectedLine" : "";
					};
					trRecord.onmousedown = function () {
						this.inputTarget.booleanScan = true;
						this.inputTarget.setLine(this);
					};
				}
				element.divHint.innerHTML = "";
				element.divHint.appendChild(tableContainer);
				element.tableHint = tableContainer;
			}
			else {
				element.divHint.style.display = "none";
				element.divHint.innerHTML = "";
				element.tableHint = null;
			}
		}
	};
	this.bind = function (element) {	
	
	    if( $("suggesCss") == null )
	    {
	        OnStylesheetInit(this.theme, "suggesCss");
	    }
	    
		element.suggest = this;
		element.show = this.show;
		element.show.element = element;
		element.intThread = -1;
		element.arrayData = new Array();
		element.value = "代码/名称/拼音";
		element.style.color = "#999999";
		element.booleanScan = true;
		element.autocomplete = "off";
		var divDataTable = document.createElement("div");
		divDataTable.style.display = "none";
		element.parentNode.insertBefore(divDataTable, element);
		element.divHint = divDataTable;
		element.tableHint = null;
		element.line = null;
		element.overLine = null;
		divDataTable.className = "suggest";
		element.onfocus = function () {
			if (this.value == "代码/名称/拼音") {
				this.value = "";
				this.code = "";
				this.style.color = "";
			};
			this.stringLastValue = this.value;
			if (this.divHint.innerHTML != "") {
				this.divHint.style.display = "block";
				var arrayPosition = GetOffsetPos(this);
				this.divHint.style.left = arrayPosition[0] + "px";
				this.divHint.style.top = arrayPosition[1] + "px";
				this.divHint.style.marginTop = this.clientHeight + 1 + "px";
				this.divHint.style.width = (this.clientWidth < 200 ? 200 : this.clientWidth) + "px";
				
			}
			this.select();
			this.intThread = setInterval(this.show, 10);
		};
		element.onblur = function () {
			if (this.value == "") {
				this.value = "代码/名称/拼音";
				this.style.color = "#999999";
			};
			this.divHint.style.display = "none";
			clearInterval(this.intThread);
			this.intThread = -1;
		};
		element.setLine = function (line) {
			var stringKeyFore = "";
			if (this.value.indexOf(",") != -1 && this.value.indexOf(",") != 0 && this.value.indexOf(",") != this.value.length - 1) {
				var arrayStringKeyFore = this.value.split(",");
				arrayStringKeyFore[arrayStringKeyFore.length - 1] = "";
				stringKeyFore = arrayStringKeyFore.join(",");
			}
			if (this.line != null) {
				this.line.className = this.overLine == this.line ? "overLine" : "";
			}
			this.line = line;
			line.className = this.overLine == line ? "overSelectedLine" : "selectedLine";
			this.value = line.stringCodeAndName;
			this.code = line.stringFullCode;
		};
		element.onkeydown = function () {
			if (!this.tableHint) {
				return true;
			}
			var stringKeyFore = "";
			if (this.value.indexOf(",") != -1 && this.value.indexOf(",") != 0 && this.value.indexOf(",") != this.value.length - 1) {
				var arrayStringKeyFore = this.value.split(",");
				arrayStringKeyFore[arrayStringKeyFore.length - 1] = "";
				stringKeyFore = arrayStringKeyFore.join(",");
			}
			
			var event = arguments[0] || window.event;
			switch (event.keyCode) {
				case 38: //up
					this.booleanScan = false;
					if (this.line == null || this.line.rowIndex == 1) {
						this.setLine(this.tableHint.rows[this.tableHint.rows.length - 1]);
					}
					else {
						this.setLine(this.tableHint.rows[this.line.rowIndex - 1]);
					}
					return false;
					break;
				case 40: //down
					this.booleanScan = false;
					if (this.line == null || this.line.rowIndex == this.tableHint.rows.length - 1) {
						this.setLine(this.tableHint.rows[1]);
					}
					else {
						this.setLine(this.tableHint.rows[this.line.rowIndex + 1]);
					}
					return false;
					break;
				case 13: //Enter
					this.booleanScan = true;
					this.stringLastValue = stringKeyFore + this.value;
					this.divHint.style.display = "none";
					break;
				default:
					this.booleanScan = true;
					break;
			}
		};
	};

	var model = new Model("script");
	model.suggest = this;
	this.model = model;
	model.stringUrl = stringUrl ? stringUrl : "suggest.js";
	model.processData = function () {
		var suggest = this.suggest;
		suggest.stringData = suggest.stringRecordSpliter + window["SuggestData"];
		var arrayStringSystemKeys = suggest.stringSystemKeys.split(",");
		for (var i =0; i < arrayStringSystemKeys.length; i++) {
			var stringNearestData = suggest.getNearestData(arrayStringSystemKeys[i]);
			var arrayResult = stringNearestData == "" ?  suggest.getResult(suggest.stringData, arrayStringSystemKeys[i]) : suggest.getResult(stringNearestData, arrayStringSystemKeys[i]);
			arrayResult = arrayResult == null ? new Array() : arrayResult;
			suggest.arraySystemKeys.push(new Array(arrayStringSystemKeys[i], arrayResult.join("")));
		}
	};
	model.load();
};
